﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Windows.Browser;

using SBPweb.PROvibe.Client.Framework.Contracts.Parameters;

namespace SBPweb.PROvibe.Client.Framework.Actors.Parameters
{

    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/SBPweb.PROvibe.Shared.Actors")]
    public class EntryInitializationParameter : IEntryInitializationParameter
    {

        public EntryInitializationParameter()
        {
        }

        public EntryInitializationParameter(object value)
        {
            Value = value;
        }

        [DataMember]
        public object Value
        {
            get;
            set;
        }

        //public static string Serialize(EntryInitializationParameter parameter)
        //{
        //    DataContractSerializer dcs = new DataContractSerializer(typeof(EntryInitializationParameter));
        //    XmlWriter xW = XmlWriter.Create(new StringWriter());

        //    dcs.WriteObject(xW, parameter);
        //    return xW.ToString();
        //}

        public static IEntryInitializationParameter Deserialize(string serialized)
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(EntryInitializationParameter));
            XmlReader xdr = XmlDictionaryReader.Create(new StringReader(Decrypt(serialized)));
            return dcs.ReadObject(xdr, false) as IEntryInitializationParameter;
        }

        private static string Decrypt(string stuff)
        {
            //Aes aes = new AesManaged();
            //aes.Key = Encoding.UTF8.GetBytes("SBPweb.PROvibe.S");
            //ICryptoTransform ict = aes.CreateDecryptor();
            //byte[] bytesIn = Encoding.Unicode.GetBytes(stuff);
            //byte[] bytesOut = (byte[])Array.CreateInstance(typeof(byte), bytesIn.Length);
            //ict.TransformBlock(bytesIn, 0, aes.BlockSize, bytesOut, 0);
            //string ret = HttpUtility.UrlDecode(Encoding.Unicode.GetString(bytesOut, 0, bytesOut.Length));
            //return ret;
            return stuff;
        }

    }

    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/SBPweb.PROvibe.Shared.Actors")]
    public class EntryInitializationParameter<TValue> : EntryInitializationParameter, IEntryInitializationParameter<TValue>
    {

        public EntryInitializationParameter()
            : base()
        {
        }

        public EntryInitializationParameter(TValue value)
            : base(value)
        {
            Value = value;
        }

        [DataMember]
        public new TValue Value
        {
            get
            {
                return (TValue)base.Value;
            }
            set
            {
                base.Value = value;
            }
        }

    }

}